<?php
/**
*   师徒关系
*/
namespace app\api\model;
use think\Model;

class Mentor extends Base
{
    /**
    * 获取师徒产生的总收益
    */
    public function _getTotalProfit($pid, $cid){
        $data = $this->field('profit')->where([['pid', '=', $pid], ['cid', '=', $cid]])->find();
        return empty($data) ? 0 : $data['profit'];
    }

    /**
    * 校验是否为第一位好友
    */
    public function _isFirst($pid, $cid){
        $data = $this->where([['pid', '=', $pid]])->order("created")->find();
        if(empty($data)){
            return false;
        }
        
        if($data['cid'] != $cid){
            return false;
        }
        
        return true;
    }
    
    
    /**
    * 我的徒弟
    * @param pid int 会员id
    */
    
    public function _getApprenticeAll($pid){
        return $this->alias('me')
            ->field('m.id, ml.title as level_name, t.token, mp.image, mp.nick_name, mp.sex, mp.pid as pdd, mp.cid, mp.address,me.created, m.status, m.unique, me.profit')
            ->join("member m", 'm.id=me.cid', 'LEFT')
            ->join('token t', 'm.id=t.mid', 'LEFT')
            ->join('member_level ml', 'm.level_id=ml.id', 'LEFT')
            ->join('member_profile mp', 'm.id=mp.mid', 'LEFT')
            ->join('member_info mi', 'm.id=mi.mid', 'LEFT')
            ->where([['me.pid', '=', $pid]])
            ->select();
    }
}